/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.algorithm
 * author:David
 * Date:2015年8月17日下午3:13:13
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.algorithm;

/**
 * 字符串操作的一些算法
 * @author David
 * @since 2015年8月17日 下午3:13:13
 * @version 
 * @since JDK 1.6
 */
public class StringOper {
	public static void main(String[] args) {
//		String str="__sgsgs___sgsgesr_sgesg_sgesg____gsgsr__gege__";
//		String str="_g__s___";
//		String res=compressStrngBlank(str);
//		System.out.println(res); 
		
//		String str="2342SDSsdsdax@!$%^&*()_-=+/?<>``~";
//		str2ASII(str);
		int num=str2Int("214748367");
		System.out.println(num+1090);
		
	}
	

	/**
	 * 把字符串中的连续出现空格压缩为一个，
	 * @param str
	 * @return
	 */
	public static String compressStrngBlank(String str) {
		char[] toCh=str.toCharArray();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<toCh.length;){
			sb.append(toCh[i]+"");
			if(i+1==toCh.length){
				break;
			}
			while ("_".equals(toCh[i] + "")) {
				if (i + 1 == toCh.length) {
					break;
				}
				if (toCh[i + 1] == toCh[i]) {
					i++;
				} else {
					break;
				}
			}
			i++;
			
		}
		return sb.toString();
	}
	
	/**
	 * 字符串转ASCII
	 */
	public static String str2ASII(String str) {
		char[]toCh=str.toCharArray();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<toCh.length;i++){
			int tmp=toCh[i];
			sb.append(tmp);
		}
		return sb.toString();
		
	}
	
	/**
	 *字符串转数字
	 */
	public static int str2Int(String str){
		char[] toCh=str.toCharArray();
		for(int i=0;i<toCh.length;i++){
			int check=toCh[i];
			if(check<48||check>57){
				throw new NumberFormatException();
			}
		}
		int[]tmp=new int[toCh.length];
		int num=0;
		for(int i=toCh.length-1;i>=0;i--){
			switch (toCh[i]) {
			case '0':tmp[i]=0;
			break;
			case '1':tmp[i]=1;
			break;
			case '2':tmp[i]=2;
			break;
			case '3':tmp[i]=3;
			break;
			case '4':tmp[i]=4;
			break;
			case '5':tmp[i]=5;
			break;
			case '6':tmp[i]=6;
			break;
			case '7':tmp[i]=7;
			break;
			case '8':tmp[i]=8;
			break;
			default:tmp[i]=9;
				break;
			}
			int n=1;
			for(int k=1;k<=toCh.length-i-1;k++){
				n*=10;
			}
			num+=tmp[i]*n;
		}
		return num;
	}

}
